package com.jjyu.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.jjyu.entity.Friend;
import com.jjyu.entity.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface FriendMapper extends BaseMapper<Friend> {
    //联表查询User信息，将User信息放到friend对象中
    @Select("SELECT f.user_id as user_id,f.friend_id as friend_id,f.created_at as created_at, " +
            "u1.name AS friend_name,u1.email AS friend_email,  " +
            "u2.name AS user_name, u2.email AS user_email  " +
            "FROM friend f LEFT JOIN " +
            "    user u1 ON f.friend_id = u1.user_id" +
            " LEFT JOIN " +
            "    user u2 ON f.user_id = u2.user_id" +
            " WHERE f.user_id = #{userId}")
    @Results(
            {
                    @Result(column = "friend_id", property = "friend.userId"),
                    @Result(column = "friend_id", property = "friendId"),
                    @Result(column = "friend_name", property = "friend.name"),
                    @Result(column = "friend_email", property = "friend.email"),
                    @Result(column = "user_id", property = "user.userId"),
                    @Result(column = "user_id", property = "userId"),
                    @Result(column = "user_name", property = "user.name"),
                    @Result(column = "user_email", property = "user.email"),
            }
    )
    List<Friend> getAllFriendInfo(int userId);
}